package com.yonyou.pmclouds.billtype.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.yonyou.pmclouds.ApplicationConsts;
import com.yonyou.pmclouds.basecom.consts.BillTypeConst;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.basecom.exception.IllegalRequestArgException;
import com.yonyou.pmclouds.billtype.entity.BillTypeRefVO;
import com.yonyou.pmclouds.billtype.entity.BillTypeVO;
import com.yonyou.pmclouds.billtype.rmiitf.BillTypeMaintain;
import com.yonyou.pmclouds.billtype.rmiitf.BillTypeQuery;
import com.yonyou.pmclouds.businessobject.entity.BusinessObjectVO;
import com.yonyou.pmclouds.milestone.service.IBillTypeRefService;
import com.yonyou.pmclouds.outter.nc.NcBillTypeCustomerQuery;
import com.yonyou.pmclouds.project.entity.ProjectVO;
import com.yonyou.pmclouds.project.rmiitf.ProjectSimpleQuery;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 单据类型(目前只用到了查询，其他接口暂时保留)
 *
 * @author yzx
 */
@RestController
@RequestMapping("/billType")
public class BillTypeController {
    @Reference(version = ApplicationConsts.APPLICATION_VERSION)
    private BillTypeMaintain maintain;
    @Reference(version = ApplicationConsts.APPLICATION_VERSION)
    private BillTypeQuery billTypeQuery;
    @Reference(version = ApplicationConsts.APPLICATION_VERSION)
    private IBillTypeRefService billTypeRefService;
    @Autowired
    private NcBillTypeCustomerQuery ncBillTypeCustomerQuery;
    @Reference(version = ApplicationConsts.APPLICATION_VERSION)
    private ProjectSimpleQuery projectSimpleQuery;

    @PostMapping("/add")
    public BillTypeVO add(@RequestBody @Validated BillTypeVO billTypeVO, BindingResult errors) throws BusinessException {
        if (errors.hasErrors()) {
            throw new IllegalRequestArgException();
        }
        return maintain.insert(billTypeVO);
    }

    @PostMapping("/delete")
    public void delete(@RequestBody @Validated BillTypeVO billTypeVO) throws BusinessException{
        maintain.delete(billTypeVO);
    }

    @PostMapping("/update")
    public BillTypeVO update(@RequestBody @Validated BillTypeVO billTypeVO, BindingResult errors) throws BusinessException {
        if (errors.hasErrors()) {
            throw new IllegalRequestArgException();
        }
        maintain.update(billTypeVO);
        return billTypeVO;
    }
    @GetMapping("/queryBillType")
    public List<BillTypeVO> getBillTypeVoListBy(String pkTenant) throws BusinessException {
        return billTypeQuery.getBillTypeVoListByPkTenant(pkTenant);
    }

    @GetMapping("/getBusinessObjsRelation")
    public List<BusinessObjectVO> getBusinessObjsRelation(String pkTenant) throws BusinessException {
        return billTypeQuery.constructBusinessObjsRelation(pkTenant);
    }

    /**
     * 获取单据类型参照集合，供计划等引用
     *
     * @return
     * @throws BusinessException
     */
    @RequestMapping(value = "/refQuery")
    public List<BillTypeRefVO> getBillTypeRef(@RequestParam(value = "pkProject", required = false) String pkProject) throws BusinessException {
        List<BillTypeRefVO> result = new ArrayList<>();
        result.add(new BillTypeRefVO(BillTypeConst.WORKSTART_BILLTYPE, BillTypeConst.WORKSTART_BILLTYPE_NAME));
        result.add(new BillTypeRefVO(BillTypeConst.COMPREHENSIVE_ACCEPTANCE_BILLTYPE, BillTypeConst.COMPREHENSIVE_ACCEPTANCE_BILLTYPE_NAME));
        result.add(new BillTypeRefVO(BillTypeConst.STAGECHECK_BILLTYPE, BillTypeConst.STAGECHECK_BILLTYPE_NAME));
        if (StringUtils.isNotEmpty(pkProject)) {
            ProjectVO projectVO = projectSimpleQuery.queryProject(pkProject);
            if (projectVO != null && StringUtils.isNotEmpty(projectVO.getPkSrc())) {
                BillTypeRefVO[] ncBillTypeVOs = ncBillTypeCustomerQuery.queryRelatedBillTypeForPlan();
                if (ArrayUtils.isNotEmpty(ncBillTypeVOs)) {
                    result.addAll(Arrays.asList(ncBillTypeVOs));
                }
            }
        } else {
            BillTypeRefVO[] ncBillTypeVOs = ncBillTypeCustomerQuery.queryRelatedBillTypeForPlan();
            if (ArrayUtils.isNotEmpty(ncBillTypeVOs)) {
                result.addAll(Arrays.asList(ncBillTypeVOs));
            }
        }

        return result;
    }
}
